package net.xju3_4.dao;

import net.xju3_4.common.utils.JdbcUtilsV1;
import net.xju3_4.entity.PaymentEntity;

import java.math.BigDecimal;
import java.util.Map;

public class PaymentDao {

    /**
     * 插入支付记录
     */
    public boolean insert(PaymentEntity payment) {
        String sql = "INSERT INTO payment (order_id, payment_no, payment_type, amount, payment_time, status) VALUES (?, ?, ?, ?, ?, ?)";

        try {
            int result = JdbcUtilsV1.executeUpdate(sql,
                payment.getOrderId(),
                payment.getPaymentNo(),
                payment.getPaymentType(),
                payment.getAmount(),
                new java.sql.Timestamp(payment.getPaymentTime().getTime()),
                payment.getStatus()
            );
            return result > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

//    查找一共盈利了多少钱，多少笔交易
    public Map<String, Integer> count() {
        String sql = "select sum(amount) as total,count(id) as count from payment";
        int totalPaymentCount = JdbcUtilsV1.executeQueryOneValue(sql);
        String sql1 = "select sum(amount) as total,count(id) as count from payment where status=1";
        int activePaymentCount = JdbcUtilsV1.executeQueryOneValue(sql1);
        String sql2 = "select sum(amount) as total,count(id) as count from payment where status=0";
        int forbidPaymentCount = JdbcUtilsV1.executeQueryOneValue(sql2);
        return Map.of("totalPayment",totalPaymentCount,"activePayment",activePaymentCount,"forbidPayment",forbidPaymentCount);

    }

    public Map<String, Integer> count1() {
        String sqlTotalAmount = "select sum(amount) from payment";
        BigDecimal totalPaymentAmount = (BigDecimal) JdbcUtilsV1.executeQueryOneValue(sqlTotalAmount);

        String sqlTotalCount = "select count(id) from payment";
        Integer totalCount = ((Number) JdbcUtilsV1.executeQueryOneValue(sqlTotalCount)).intValue();

        String sqlActiveAmount = "select sum(amount) from payment where status=1";
        BigDecimal activePaymentAmount = (BigDecimal) JdbcUtilsV1.executeQueryOneValue(sqlActiveAmount);

        String sqlActiveCount = "select count(id) from payment where status=1";
        Integer activeCount = ((Number) JdbcUtilsV1.executeQueryOneValue(sqlActiveCount)).intValue();

        String sqlForbidAmount = "select sum(amount) from payment where status=2";
        BigDecimal forbidPaymentAmount = (BigDecimal) JdbcUtilsV1.executeQueryOneValue(sqlForbidAmount);

        String sqlForbidCount = "select count(id) from payment where status=0";
        Integer forbidCount = ((Number) JdbcUtilsV1.executeQueryOneValue(sqlForbidCount)).intValue();

//        return Map.of(
//                "totalPayment", totalPaymentAmount.intValue(),
//                "activePayment", activePaymentAmount.intValue(),
//                "forbidPayment", forbidPaymentAmount.intValue()
//        )
        return Map.of(
                "totalPayment", totalPaymentAmount != null ? totalPaymentAmount.intValue() : 0,
                "activePayment", activePaymentAmount != null ? activePaymentAmount.intValue() : 0,
                "forbidPayment", forbidPaymentAmount != null ? forbidPaymentAmount.intValue() : 0  // 修复点：添加空检查
        );

    }

}





